package com.zyzd.pathdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.graphics.PathMeasure;
import android.graphics.SumPathEffect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by 李宗源 on 2017/5/4.
 */

public class PathEffectView extends View {

    private Paint mPaint;
    private int marging = 82;
    private CornerPathEffect mCornerPathEffect[];
    private DashPathEffect mDashPathEffect[];
    private DiscretePathEffect mDiscretePathEffect[];
    private Path mPaths[];
    private Path mPath;
    private PathDashPathEffect mPathEffect1;
    private PathDashPathEffect mPathEffect2;
    private PathDashPathEffect mPathEffect3;
    private PathEffect[] mEffects;

    public PathEffectView(Context context) {
        this(context, null);
    }

    public PathEffectView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PathEffectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
//        initOfCornerPath();
//        initOfDashPath();
//        initOfDiscretePath();
//        initOfPathDashPath();
        initOfComposePath();
    }

    /**
     * CornerPathEffect将Path的线段之间的夹角变成圆角。构造函数，其中radius为圆角的半径
     */
    private void initOfCornerPath() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        mCornerPathEffect = new CornerPathEffect[8];
        mPaths = new Path[8];
        for (int i = 0; i < mPaths.length; i++) {
            Path path = new Path();
            path.moveTo(i * marging, marging);
            path.lineTo(300 + i * marging, 180);
            path.lineTo(400 + i * marging, 600);
            path.lineTo(200 + i * marging, 1000);
            path.lineTo(110 + i * marging, 1200);
            mPaths[i] = path;
            mCornerPathEffect[i] = new CornerPathEffect(i * 10);
        }
    }

    /**
     * DashPathEffect主要用于画虚线。构造函数，看注释，intervals必须大于大于2，phase是偏移量
     */
    private void initOfDashPath() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        mDashPathEffect = new DashPathEffect[8];
        mPaths = new Path[8];
        for (int i = 0; i < mPaths.length; i++) {
            Path path = new Path();
            path.moveTo(i * marging, marging);
            path.lineTo(300 + i * marging, 180);
            path.lineTo(400 + i * marging, 600);
            path.lineTo(200 + i * marging, 1000);
            path.lineTo(110 + i * marging, 1200);
            mPaths[i] = path;
//            mDashPathEffect[i] = new DashPathEffect(
//                    new float[]{10, 20, 40,
//                    80}, 1);
            mDashPathEffect[i] = new DashPathEffect(new float[]{10, 20, 40,
                    80}, i * 10);//第一个数组参数
        }
    }

    /**
     * DiscretePathEffect切断线段，segmentLength是指定切断的长度，deviation为切断之后线段的偏移量，随机的，小于等于deviation。
     */
    private void initOfDiscretePath() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        mDiscretePathEffect = new DiscretePathEffect[8];
        mPaths = new Path[8];
        for (int i = 0; i < mPaths.length; i++) {
            Path path = new Path();
            path.moveTo(i * marging, marging);
            path.lineTo(300 + i * marging, 180);
            path.lineTo(400 + i * marging, 600);
            path.lineTo(200 + i * marging, 1000);
            path.lineTo(110 + i * marging, 1200);
            mPaths[i] = path;
//            mDiscretePathEffect[i] = new DiscretePathEffect(10, 3 * i);
            mDiscretePathEffect[i] = new DiscretePathEffect(1, 3 * i);
        }
    }

    /**
     * (1) shape    是填充的图形，这个图形可以自己绘制
     * (2)advance  是图形之间的间距
     * (3)phase    是path的偏移量，其中有3种style
     * <p>
     * <p>
     */
    private void initOfPathDashPath() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        mPaint.setColor(Color.RED);
        mPath = new Path();
        mPath.moveTo(0, marging);
        mPath.lineTo(300, 180);
        mPath.lineTo(400, 600);
        mPath.lineTo(200, 1000);
        mPath.lineTo(800, 1200);
        Path p = new Path();
        p.addRect(0, 0, 64, 12, Path.Direction.CCW);

        //(3-1)TRANSLATE是指图形以平移的方式填充path
        //(3-2)ROTATE会根据path的旋转而旋转
        //(3-3)MORPH和ROTATE差不多，不过有一点就是MORPH会在转角的连接处以平滑的方式连接

//        mPathEffect1 = new PathDashPathEffect(p, 128, 0,
//                android.graphics.PathDashPathEffect.Style.MORPH);
//        mPathEffect2 = new PathDashPathEffect(p, 128, 0,
//                android.graphics.PathDashPathEffect.Style.ROTATE);
//        mPathEffect3 = new PathDashPathEffect(p, 128, 0,
//                android.graphics.PathDashPathEffect.Style.TRANSLATE);


        // 第一个是没有间距的，第二个间距等于矩形的宽度，所以正好相当于矩形首尾相连中间没有间隙，
        // 最后一个有间隙。
//        mPathEffect1 = new PathDashPathEffect(p, 0, 0,
//                android.graphics.PathDashPathEffect.Style.MORPH);
//        mPathEffect2 = new PathDashPathEffect(p, 64, 0,
//                android.graphics.PathDashPathEffect.Style.MORPH);
//        mPathEffect3 = new PathDashPathEffect(p, 228, 0,
//                android.graphics.PathDashPathEffect.Style.MORPH);

        //再看第三个参数，就是偏移量，这个和第二个参数有关，在1到advance中间时，
        // 偏移的距离在逐渐减少，当偏移量等于advance的倍数的时候，偏移的距离为0，当偏移量大于advance
        // 的时候，会对他求余。
        mPathEffect1 = new PathDashPathEffect(p, 128, 128,
                android.graphics.PathDashPathEffect.Style.MORPH);
        mPathEffect2 = new PathDashPathEffect(p, 128, 129,
                android.graphics.PathDashPathEffect.Style.MORPH);
        mPathEffect3 = new PathDashPathEffect(p, 128, 1,
                android.graphics.PathDashPathEffect.Style.MORPH);
//
//        mPathEffect3 = new PathDashPathEffect(p, 28, 28,
//                android.graphics.PathDashPathEffect.Style.MORPH);
    }

    /**
     * ComposePathEffect是一种组合模式，把两种path所具有的特性组合起来
     * 第一个是圆角的，第二个是虚线的，所以组合的第三个就是圆角到虚线的
     */
    private void initOfComposePath() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        mPaint.setColor(Color.RED);
        mPath = new Path();
        mPath.moveTo(0, marging);
        mPath.lineTo(300, 180);
        mPath.lineTo(400, 600);
        mPath.lineTo(200, 1000);
        mPath.lineTo(800, 1200);
        Path p = new Path();
        p.addRect(0, 0, 64, 12, Path.Direction.CCW);
        mEffects = new PathEffect[3];
        mEffects[0] = new CornerPathEffect(80);
        mEffects[1] = new DashPathEffect(new float[] { 20, 10, 5, 10 }, 0);
//        mEffects[1] = new DashPathEffect(new float[] { 200, 10, 5, 10 }, 0);
//        mEffects[2] = new ComposePathEffect(mEffects[1], mEffects[0]);
        mEffects[2] = new SumPathEffect(mEffects[1], mEffects[0]);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        onDrawOfCornerPath(canvas);
//        onDrawOfDashPath(canvas);
//        onDrawOfDiscretePath(canvas);
//        onDrawOfPathDashPath(canvas);
        onDrawOfComposePath(canvas);
    }

    private void onDrawOfCornerPath(Canvas canvas) {
        canvas.drawColor(Color.WHITE);//设置背景色
        for (int i = 0; i < mPaths.length; i++) {
            mPaint.setPathEffect(mCornerPathEffect[i]);
            canvas.drawPath(mPaths[i], mPaint);
        }
    }

    private void onDrawOfDashPath(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        for (int i = 0; i < mPaths.length; i++) {
            mPaint.setPathEffect(mDashPathEffect[i]);
            canvas.drawPath(mPaths[i], mPaint);
            PathMeasure measure = new PathMeasure(mPaths[i], false);
            Log.d("wld_________", measure.getLength() + "");//再次可以看出长度是一样的，所以所看到的类似后面的长度类似变短是假象，其实质是偏移了，即类似绳子被往后拽了
        }
    }

    private void onDrawOfDiscretePath(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        for (int i = 0; i < mPaths.length; i++) {
            mPaint.setPathEffect(mDiscretePathEffect[i]);
            canvas.drawPath(mPaths[i], mPaint);
            PathMeasure measure = new PathMeasure(mPaths[i], false);
            Log.d("wld__________", measure.getLength() + "");
        }
    }

    private void onDrawOfPathDashPath(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        mPaint.setPathEffect(mPathEffect1);
        canvas.drawPath(mPath, mPaint);
        canvas.translate(200, 0);
        mPaint.setPathEffect(mPathEffect2);
        canvas.drawPath(mPath, mPaint);
        canvas.translate(200, 0);
        mPaint.setPathEffect(mPathEffect3);
        canvas.drawPath(mPath, mPaint);
    }

    private void onDrawOfComposePath(Canvas canvas) {
        for (int i = 0; i < mEffects.length; i++) {
            mPaint.setPathEffect(mEffects[i]);
            canvas.drawPath(mPath, mPaint);
            canvas.translate(200, 0);
        }
    }
}
